home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gxhtbit.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  7.9 KB  |  277 lines

  1. /* Copyright (C) 1999, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gxhtbit.c,v 1.3 2000/09/19 19:00:37 lpd Exp $ */
  20. /* Halftone bit updating for imaging library */
  21. #include "memory_.h"
  22. #include "gx.h"
  23. #include "gserrors.h"
  24. #include "gsbitops.h"
  25. #include "gscdefs.h"
  26. #include "gxbitmap.h"
  27. #include "gxhttile.h"
  28. #include "gxtmap.h"
  29. #include "gxdht.h"
  30. #include "gxdhtres.h"
  31.  
  32. extern_gx_device_halftone_list();
  33.  
  34. /*
  35.  * Construct a standard-representation order from a threshold array.
  36.  */
  37. private int
  38. construct_ht_order_default(gx_ht_order *porder, const byte *thresholds)
  39. {
  40.     gx_ht_bit *bits = (gx_ht_bit *)porder->bit_data;
  41.     uint i;
  42.  
  43.     for (i = 0; i < porder->num_bits; i++)
  44.     bits[i].mask = max(1, thresholds[i]);
  45.     gx_ht_complete_threshold_order(porder);
  46.     return 0;
  47. }
  48.  
  49. /*
  50.  * Construct a short-representation order from a threshold array.
  51.  * Uses porder->width, num_levels, num_bits, levels, bit_data;
  52.  * sets porder->levels[], bit_data[].
  53.  */
  54. private int
  55. construct_ht_order_short(gx_ht_order *porder, const byte *thresholds)
  56. {
  57.     uint size = porder->num_bits;
  58.     uint i;
  59.     ushort *bits = (ushort *)porder->bit_data;
  60.     uint *levels = porder->levels;
  61.     uint num_levels = porder->num_levels;
  62.  
  63.     memset(levels, 0, num_levels * sizeof(*levels));
  64.     /* Count the number of threshold elements with each value. */
  65.     for (i = 0; i < size; i++) {
  66.     uint value = max(1, thresholds[i]);
  67.  
  68.     if (value + 1 < num_levels)
  69.         levels[value + 1]++;
  70.     }
  71.     for (i = 2; i < num_levels; ++i)
  72.     levels[i] += levels[i - 1];
  73.     /* Now construct the actual order. */
  74.     {
  75.     uint width = porder->width;
  76.     uint padding = bitmap_raster(width) * 8 - width;
  77.  
  78.     for (i = 0; i < size; i++) {
  79.         uint value = max(1, thresholds[i]);
  80.  
  81.         /* Adjust the bit index to account for padding. */
  82.         bits[levels[value]++] = i + (i / width * padding);
  83.     }
  84.     }
  85.  
  86.     /* Check whether this is a predefined halftone. */
  87.     {
  88.     const gx_dht_proc *phtrp = gx_device_halftone_list;
  89.  
  90.     for (; *phtrp; ++phtrp) {
  91.         const gx_device_halftone_resource_t *const *pphtr = (*phtrp)();
  92.         const gx_device_halftone_resource_t *phtr;
  93.  
  94.         while ((phtr = *pphtr++) != 0) {
  95.         if (phtr->Width == porder->width &&
  96.             phtr->Height == porder->height &&
  97.             phtr->elt_size == sizeof(ushort) &&
  98.             !memcmp(phtr->levels, levels, num_levels * sizeof(*levels)) &&
  99.             !memcmp(phtr->bit_data, porder->bit_data,
  100.                 size * phtr->elt_size)
  101.             ) {
  102.             /*
  103.              * This is a predefined halftone.  Free the levels and
  104.              * bit_data arrays, replacing them with the built-in ones.
  105.              */
  106.             if (porder->data_memory) {
  107.             gs_free_object(porder->data_memory, porder->bit_data,
  108.                        "construct_ht_order_short(bit_data)");
  109.             gs_free_object(porder->data_memory, porder->levels,
  110.                        "construct_ht_order_short(levels)");
  111.             }
  112.             porder->data_memory = 0;
  113.             porder->levels = (uint *)phtr->levels; /* actually const */
  114.             porder->bit_data = (void *)phtr->bit_data; /* actually const */
  115.             goto out;
  116.         }
  117.         }
  118.     }
  119.     }
  120.  out:
  121.     return 0;
  122. }
  123.  
  124. /* Return the bit coordinate using the standard representation. */
  125. private int
  126. ht_bit_index_default(const gx_ht_order *porder, uint index, gs_int_point *ppt)
  127. {
  128.     const gx_ht_bit *phtb = &((const gx_ht_bit *)porder->bit_data)[index];
  129.     uint offset = phtb->offset;
  130.     int bit = 0;
  131.  
  132.     while (!(((const byte *)&phtb->mask)[bit >> 3] & (0x80 >> (bit & 7))))
  133.     ++bit;
  134.     ppt->x = (offset % porder->raster * 8) + bit;
  135.     ppt->y = offset / porder->raster;
  136.     return 0;
  137. }
  138.  
  139. /* Return the bit coordinate using the short representation. */
  140. private int
  141. ht_bit_index_short(const gx_ht_order *porder, uint index, gs_int_point *ppt)
  142. {
  143.     uint bit_index = ((const ushort *)porder->bit_data)[index];
  144.     uint bit_raster = porder->raster * 8;
  145.  
  146.     ppt->x = bit_index % bit_raster;
  147.     ppt->y = bit_index / bit_raster;
  148.     return 0;
  149. }
  150.  
  151. /* Update a halftone tile using the default order representation. */
  152. private int
  153. render_ht_default(gx_ht_tile *pbt, int level, const gx_ht_order *porder)
  154. {
  155.     int old_level = pbt->level;
  156.     register const gx_ht_bit *p =
  157.     (const gx_ht_bit *)porder->bit_data + old_level;
  158.     register byte *data = pbt->tiles.data;
  159.  
  160.     /*
  161.      * Invert bits between the two levels.  Note that we can use the same
  162.      * loop to turn bits either on or off, using xor.  The Borland compiler
  163.      * generates truly dreadful code if we don't use a temporary, and it
  164.      * doesn't hurt better compilers, so we always use one.
  165.      */
  166. #define INVERT_DATA(i)\
  167.      BEGIN\
  168.        ht_mask_t *dp = (ht_mask_t *)&data[p[i].offset];\
  169.        *dp ^= p[i].mask;\
  170.      END
  171. #ifdef DEBUG
  172. #  define INVERT(i)\
  173.      BEGIN\
  174.        if_debug3('H', "[H]invert level=%d offset=%u mask=0x%x\n",\
  175.              (int)(p + i - (const gx_ht_bit *)porder->bit_data),\
  176.          p[i].offset, p[i].mask);\
  177.        INVERT_DATA(i);\
  178.      END
  179. #else
  180. #  define INVERT(i) INVERT_DATA(i)
  181. #endif
  182.   sw:switch (level - old_level) {
  183.     default:
  184.         if (level > old_level) {
  185.         INVERT(0); INVERT(1); INVERT(2); INVERT(3);
  186.         p += 4; old_level += 4;
  187.         } else {
  188.         INVERT(-1); INVERT(-2); INVERT(-3); INVERT(-4);
  189.         p -= 4; old_level -= 4;
  190.         }
  191.         goto sw;
  192.     case 7: INVERT(6);
  193.     case 6: INVERT(5);
  194.     case 5: INVERT(4);
  195.     case 4: INVERT(3);
  196.     case 3: INVERT(2);
  197.     case 2: INVERT(1);
  198.     case 1: INVERT(0);
  199.     case 0: break;        /* Shouldn't happen! */
  200.     case -7: INVERT(-7);
  201.     case -6: INVERT(-6);
  202.     case -5: INVERT(-5);
  203.     case -4: INVERT(-4);
  204.     case -3: INVERT(-3);
  205.     case -2: INVERT(-2);
  206.     case -1: INVERT(-1);
  207.     }
  208. #undef INVERT_DATA
  209. #undef INVERT
  210.     return 0;
  211. }
  212.  
  213. /* Update a halftone tile using the short representation. */
  214. private int
  215. render_ht_short(gx_ht_tile *pbt, int level, const gx_ht_order *porder)
  216. {
  217.     int old_level = pbt->level;
  218.     register const ushort *p = (const ushort *)porder->bit_data + old_level;
  219.     register byte *data = pbt->tiles.data;
  220.  
  221.     /* Invert bits between the two levels. */
  222. #define INVERT_DATA(i)\
  223.      BEGIN\
  224.        uint bit_index = p[i];\
  225.        byte *dp = &data[bit_index >> 3];\
  226.        *dp ^= 0x80 >> (bit_index & 7);\
  227.      END
  228. #ifdef DEBUG
  229. #  define INVERT(i)\
  230.      BEGIN\
  231.        if_debug3('H', "[H]invert level=%d offset=%u mask=0x%x\n",\
  232.              (int)(p + i - (const ushort *)porder->bit_data),\
  233.          p[i] >> 3, 0x80 >> (p[i] & 7));\
  234.        INVERT_DATA(i);\
  235.      END
  236. #else
  237. #  define INVERT(i) INVERT_DATA(i)
  238. #endif
  239.   sw:switch (level - old_level) {
  240.     default:
  241.         if (level > old_level) {
  242.         INVERT(0); INVERT(1); INVERT(2); INVERT(3);
  243.         p += 4; old_level += 4;
  244.         } else {
  245.         INVERT(-1); INVERT(-2); INVERT(-3); INVERT(-4);
  246.         p -= 4; old_level -= 4;
  247.         }
  248.         goto sw;
  249.     case 7: INVERT(6);
  250.     case 6: INVERT(5);
  251.     case 5: INVERT(4);
  252.     case 4: INVERT(3);
  253.     case 3: INVERT(2);
  254.     case 2: INVERT(1);
  255.     case 1: INVERT(0);
  256.     case 0: break;        /* Shouldn't happen! */
  257.     case -7: INVERT(-7);
  258.     case -6: INVERT(-6);
  259.     case -5: INVERT(-5);
  260.     case -4: INVERT(-4);
  261.     case -3: INVERT(-3);
  262.     case -2: INVERT(-2);
  263.     case -1: INVERT(-1);
  264.     }
  265. #undef INVERT_DATA
  266. #undef INVERT
  267.     return 0;
  268. }
  269.  
  270. /* Define the procedure vectors for the order data implementations. */
  271. const gx_ht_order_procs_t ht_order_procs_table[2] = {
  272.     { sizeof(gx_ht_bit), construct_ht_order_default, ht_bit_index_default,
  273.       render_ht_default },
  274.     { sizeof(ushort), construct_ht_order_short, ht_bit_index_short,
  275.       render_ht_short }
  276. };
  277.